<template>
    <!-- use the modal component, pass in the prop -->
    <modal v-if="stateManager.editDialogVisible"
           @cancel="closeDialog"
           @ok="saveModel"
           :modal-title="modalTitle"
           model-size="normal">
        <!--
          you can use custom content here to overwrite
          default content
        -->
        <vue-form>
            <div class="row">
                <div class="col-xs-12">
                    <vue-form-item label="视频标题" :error-message="model.errors.videoTitle || errors.first('videoTitle')">
                        <input-text v-model:value="model.videoTitle"
                                    label="视频标题"
                                    el-name="videoTitle"></input-text>
                    </vue-form-item>
                    <vue-form-item label="视频简介" :error-message="model.errors.videoDescribe || errors.first('videoDescribe')">
                        <input-text v-model:value="model.videoDescribe"
                                    label="视频简介"
                                    el-name="videoDescribe"></input-text>
                    </vue-form-item>
                </div>
            </div>
        </vue-form>


    </modal>


</template>

<script>
    export default {
        props: {
            stateManager: {
                type: Object
            }
        },
        data: function () {
            return {
                model: this.$modelDataSource({
                    url: 'api/WeChat/WeChatVideo',
                    dataKey: 'model',
                    attributes: {
                        id: 0,
                        videoTitle:'',
                        videoDescribe:''
                    }
                })
            };
        },
        methods: {
            closeDialog: function () {
//                console.log(11);
                this.stateManager.editDialogVisible = false;
                this.stateManager.editModelId = 0;

            },
            saveModel: function () {
//                ladda.start();
                let self = this;
               // console.log(self.validate());
                self.validate(function (){
                        self.model.save(function () {
                            self.model.reset();
                            self.closeDialog();
                            self.stateManager.refreshList = true;
                            toastr.success('保存成功！');
                        }, function () {
                            if (self.model.errors.permissions_errors) {
                                toastr.error(self.model.errors.permissions_errors);
                            }
                        });

            })
            },
//            checkName:function (name) {
//                return /^(.+?)\.(mp4|MP4)$/.test(name);
//            },
            validate: function (successCallback, failedCallback) {
                this.$validator.validateAll({qq:this.model.imgName}).then(() => {
                    // eslint-disable-next-line
                    console.log('验证通过');
                    typeof successCallback === 'function' && successCallback();
                }).catch(() => {
                    // eslint-disable-next-line
                    console.log('验证未通过');
                    typeof failedCallback === 'function' && failedCallback();
                });

            }
        },
        computed: {
            modalTitle: function () {
                return '视频详情';
            }
        },
        watch: {
            'stateManager.editModelId': function (newValue) {
                this.model.fetch(newValue);
            }
        }
    }
</script>